package demo3;

import java.util.Objects;

class Person {
    public String name;
    public Person(String name) {
        this.name = name;
    }
    /**
     * 如果想要打印自己想要的结果可以通过重写toString
     */
    @Override
    public String toString() {
        return "name" + this.name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
class Teacher extends Person {

    public Teacher(String name) {
        super(name);
    }
}
public class Test {
    public static void main(String[] args) {
        /**
         * hashCode:找到对象位置,如果相同则返回值相同
         * 可以通过Alt + Ins 快捷键,由编译器重写equals() 和hashCode()方法
         */
        Person person1 = new Person("zhangSan");
        Person person2 = new Person("zhangSan");

        System.out.println(person1.equals(person2));

        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
    }
    public static void main2(String[] args) {
        Person person1 = new Person("zhangSan");
        Person person2 = new Person("zhangSan");

        System.out.println(person1 == person2);

        //System.out.println(person1.equals(person2));//使用了Object的equals方法,结果为false
        //在Person中,重写equals方法
        System.out.println(person1.equals(person2));

        String str1 = "zhangSan";
        String str2 = "zhangSan";

        System.out.println(str1.equals(str2));
    }
    public static void main1(String[] args) {
        Object object = new Person("小黑");//使用父类引用子类实例化对象
        System.out.println(object.toString());//父类的toString是打印地址
    }
}
